package com.cn.jx.operatlog.service.Impl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.cn.jx.operatlog.entity.OperatorLog;
import com.cn.jx.operatlog.service.IOperatorLogMongoService;
import com.jx.enumType.OperatEnum;


@Service("operatorLogMongoService") 
public class OperatorLogMongoServiceImpl implements IOperatorLogMongoService {
	
	@Autowired
    private MongoTemplate mongoTemplate;
	
	@Autowired
	private HttpServletRequest req;
	
	@Override
	public void saveItem(OperatorLog item) {
		  mongoTemplate.save(item);
	}

	@Override
	public List<OperatorLog> findAllOperatorByFlag(Integer synchFlag) {
		Query query=new Query(Criteria.where("synchFlag").is(synchFlag));
		List<OperatorLog> list=mongoTemplate.find(query,  OperatorLog.class);
		return list;
	}

	@Override
	public void logMsg(OperatEnum operat,String itemName,String title,String operatUser, String... remark) {
		String returnval = "";//定义返回默认值
        if(remark!=null && remark.length>0){
        	returnval=remark[0];
    	}
		OperatorLog item=new OperatorLog();
		item.setTitle(title);
		item.setResourceItem(itemName);
		item.setOperatTime(new Date());
		item.setOperatUser(operatUser);
		item.setOperatType(operat.getCode());
		item.setOperatDesc(returnval);
		item.setSynchFlag(0);
		item.setOperatUrl(req.getRequestURL().toString());
		item.setOperatIp(getRemoteIpAddr(req));
		item.setOperatMachineName(System.getProperty("os.name"));
		item.setOperatEquipment(getEquipment(req));
		saveItem(item);
	}
	
	private static String getRemoteIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
			if (ip.indexOf(",") != -1) {
				ip = ip.split(",")[0];
			}
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("x-real-ip");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
	
	private static String getEquipment(HttpServletRequest request){
		String userAgent = request.getHeader("user-agent");
        if (userAgent.indexOf("Android") != -1) {
        	return "Android";
        } else if (userAgent.indexOf("iPhone") != -1) {
            //苹果
        	return "iPhone";
        } else if (userAgent.indexOf("iPad") != -1) {
            //苹果
        	return "iPad";
        }else {
            //电脑
        	return "PC";
        }
	}
}
